package com.teaching.Controller;


import com.alibaba.fastjson.JSONObject;
import com.teaching.Dao.StudentDaoAchieve;
import com.teaching.Dao.TeacherDaoAchieve;
import com.teaching.Enity.Student;
import com.teaching.Enity.Teacher;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class ModifyController {

    @PostMapping(value = "/modifyStudentPwd",produces = "application/json;charset=UTF-8")//学生修改密码，1成功，2原密码错误，3新旧密码相同
    public int modifyStudentPwd(@RequestBody JSONObject jsonObject) throws IOException{
        int s_id = jsonObject.getInteger("s_id");
        String old_pwd = jsonObject.getString("old_pwd");
        String new_pwd = jsonObject.getString("new_pwd");
        StudentDaoAchieve studentDaoAchieve = new StudentDaoAchieve();
        Student student = studentDaoAchieve.findStudent(s_id).get(0);
        if (old_pwd.equals(student.getPwd())){
            if (!old_pwd.equals(new_pwd)){
                student.setPwd(new_pwd);
                studentDaoAchieve.modifyStudentPwd(student);
                return 1;
            }
            else {
                return 3;
            }
        }
        else{
            return 2;
        }
    }

    @PostMapping(value = "/modifyTeacherPwd",produces = "application/json;charset=UTF-8")//教师修改密码，1成功，2原密码错误，3新旧密码相同
    public int modifyTeacherPwd(@RequestBody JSONObject jsonObject) throws IOException{
        int t_id = jsonObject.getInteger("t_id");
        String old_pwd = jsonObject.getString("old_pwd");
        String new_pwd = jsonObject.getString("new_pwd");
        TeacherDaoAchieve teacherDaoAchieve = new TeacherDaoAchieve();
        Teacher teacher = teacherDaoAchieve.findTeacher(t_id).get(0);
        if (old_pwd.equals(teacher.getPwd())){
            if (!old_pwd.equals(new_pwd)){
                teacher.setPwd(new_pwd);
                teacherDaoAchieve.modifyTeacherPwd(teacher);
                return 1;
            }
            else {
                return 3;
            }
        }
        else{
            return 2;
        }
    }

}
